//
//  WYProductServiceController.m
//  Boss
//
//  Created by admin on 2018/7/4.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYProductServiceController.h"
#import "WYProductCell.h"
#import "WYProductService.h"
@interface WYProductServiceController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UIView *backView;
@property (nonatomic,strong)NSMutableArray *dataArray;
//@property (nonatomic,strong) UIView *backView;
@property (nonatomic,assign)BOOL isRefresh;
@property (nonatomic,assign)NSUInteger page;
@end

@implementation WYProductServiceController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
   // self.view.backgroundColor = [UIColor yellowColor];
    self.page = 1;
    [self setupUI];
    [self loadData];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshdata) name:@"refreshProductService" object:nil];
    WEAKSELF
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.isRefresh=NO;
        weakSelf.page++;
        // [weakSelf loadTableData];
        [weakSelf loadData];
    }];
    
    
    
    
    
}
-(void)refreshdata
{
    self.page = 1;
    self.isRefresh = YES;
    [self loadData];
}

-(void)loadData
{
     NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    WEAKSELF
    [WYHttpServer getProductServiceWithCurrentPage:page WithPageSize:@"15" SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            if (weakSelf.isRefresh) {
                [weakSelf.dataArray removeAllObjects];
            }
            [weakSelf.dataArray addObjectsFromArray:[WYProductService getProductArrayFromJson:response[@"result"][@"detail"]]];
            [weakSelf.tableView reloadData];
            
            if (weakSelf.dataArray.count) {
                weakSelf.backView.hidden = YES;
            }else{
                weakSelf.backView.hidden = NO;
            }
            if ([response[@"result"][@"hasNextPage"] integerValue]) {
                [weakSelf.tableView.mj_footer endRefreshing];
            }else{
                weakSelf.tableView.mj_footer.hidden = YES;
            }
        }
    }];
}
-(void)setupUI
{
    [self.view addSubview:self.tableView];
//    [self.tableView registerClass:[UITableViewCell class] forHeaderFooterViewReuseIdentifier:@"cell"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYProductCell" bundle:nil] forCellReuseIdentifier:@"WYProductCell"];
    self.tableView.estimatedRowHeight = 60;
    self.tableView.tableFooterView = [[UIView alloc]init];
    UIView *backView = [[UIView alloc]init];
    self.backView = backView;
    self.backView = backView;
    [self.tableView addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(100);
        make.left.mas_equalTo(0);
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(kScreenWidth);
    }];
   // backView.backgroundColor = [UIColor blackColor];
    UIImageView *imageView = [[UIImageView alloc]init];
    [backView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.left.mas_equalTo(16);
        make.top.bottom.mas_equalTo(0);
    }];
    imageView.image = [UIImage imageNamed:@"虚框"];
    
    
    UIButton *button = [[UIButton alloc]init];
    [backView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.left.mas_equalTo(16);
        make.top.bottom.mas_equalTo(0);
    }];
    button.titleLabel.font = [UIFont systemFontOfSize:14.f];
    [button setTitle:@"添加产品服务" forState:(UIControlStateNormal)];
    [button setTitleColor:kColorMainColor forState:(UIControlStateNormal)];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [button addTarget: self action:@selector(clickAddAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
}
-(void)clickAddAction:(UIButton *)btn
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"addService" object:nil];
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (!self.vcCanScroll) {
        scrollView.contentOffset = CGPointZero;
    }
    if (scrollView.contentOffset.y <= 0) {
        self.vcCanScroll = NO;
        scrollView.contentOffset = CGPointZero;
        //到顶通知父视图改变状态
        [[NSNotificationCenter defaultCenter] postNotificationName:@"leaveTop" object:nil];
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WYProductCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYProductCell"];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYProductCell" owner:nil options:nil].firstObject;
    }
    
    WYProductService *service = self.dataArray[indexPath.row];
    [cell setService:service];
    [cell setSelectionStyle:(UITableViewCellSelectionStyleNone)];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYProductService *service = self.dataArray[indexPath.row];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"seeProduct" object:@{@"service":service}];
    
    
}
-(UITableView *)tableView
{
    if (!_tableView) {
        
        CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-self.navHeight-self.tabHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}


@end
